package com.atguigu.srb.core.controller.admin;

import com.atguigu.srb.base.result.ResponseVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/core/statistics")
public class AdminStatisticsController {
    @Resource
    RedisTemplate redisTemplate;
    @ApiOperation("查询统计分析数据")
    @GetMapping
    public ResponseVo list(){
        Map<String,Integer> educations = (Map<String, Integer>)
                redisTemplate.opsForValue().get("stat:educations");
        Map<String,Integer> incomes = (Map<String, Integer>) redisTemplate.opsForValue().get("stat:incomes");
        //r.data.educations
//        educations 每个k-v 转成一个map： name:属性名值, value: 属性值
        //              本科，5      map{ "name":"本科" , "value",5}
        List<Map<String,Object>> edus = new ArrayList<>();
        educations.forEach((k,v)->{
            Map<String,Object> map = new HashMap<>();
            map.put("name",k);
            map.put("value",v);
            edus.add(map);
        });

        List<Map<String,Object>> incs = new ArrayList<>();
        incomes.forEach((k,v)->{
            Map<String,Object> map = new HashMap<>();
            map.put("name",k);
            map.put("value",v);
            incs.add(map);
        });
        return ResponseVo.ok().data("educations",edus)
                .data("incomes",incs);
    }
}
